package edu.sctu.zhangwang.month201506.day20150616;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by user10 on 2015/6/16.
 */
public class HttpUtil {
    public String HttpUtil(String path){
        int count = 0;
        byte[] bytes = new byte[1024];
        try {
            URL url = new URL(path);
            try {
                InputStream input = url.openStream();
                while ((count=input.read(bytes)) !=-1){
                    System.out.println(new String(bytes,0,count,"utf-8"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return path;
    }
    public void HttpUtilConnectiong(String path){
        int count = 0;
        try {
            URL url = new URL("http://www.easou.com");
            byte[] bytes = new byte[1024];
            try {
                URLConnection urlConnection = url.openConnection();
                urlConnection.connect();
                InputStream in = urlConnection.getInputStream();
                while ((count=in.read(bytes))!=-1){
//                    System.out.println(new String(bytes,0,count,"utf-8"));
                }
                try {
                    System.out.println("*******:"+url.toURI()+"\t"+url.getProtocol());
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        HttpUtil httpUtil = new HttpUtil();
//        httpUtil.HttpUtilConnectiong("http://www.baidu.com");
        httpUtil.HttpUtilConnectiong("http://www.easou.com");
    }
}
